<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/24
 * Time: 16:12
 */

namespace app\admin\controller\business;


use app\common\controller\AdminController;
use think\app;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;

class OrganTeacher extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = app('app\common\model\business\OrganTeacher');
    }

    public function get_where($arr = []){
        $admin = Session::get('admin');
        $array[]=['store_id','=',$admin['store_id']];
        if(isset($arr['name']) && !empty($arr['name'])){
            $array[] = ['name','=',$arr['name']];
        }
        return $array;
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isAjax()){
            $post = $this->request->post();

            // 启动事务
            Db::startTrans();
            try {
                $rule = [];
                if (method_exists($this, 'get_check_rule')){
                    $rule = $this->get_check_rule($post ?? []);
                }
                $this->validate($post, $rule);

                if(count($post['teaching'])>3){
                    return json(['code'=>0,'msg'=>'保存失败：授课最多填写3个！']);
                }

                $post['teaching'] = implode(',',$post['teaching']);
                $admin = Session::get('admin');
                $post['store_id'] = $admin['store_id'];
                $post['app_id'] = $this->model->getApp();
                $save = $this->model->save($post);

                // 提交事务
                Db::commit();
            } catch (\Exception $e){
                Db::rollback();
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if(method_exists($this,'get_other')){
            $this->get_other($this->request->param() ?? []);
        }
        return view();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $row = $this->model->find($id);

        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            try {
                $post = $this->request->post();
                $rule = [];
                if (method_exists($this, 'get_check_rule')) {
                    $rule = $this->get_check_rule($post ?? []);
                }
                $this->validate($post, $rule);
                if(count($post['teaching'])>3){
                    return json(['code'=>0,'msg'=>'保存失败：授课最多填写3个！']);
                }
                $post['teaching'] = implode(',',$post['teaching']);
                $post['status'] = 0;
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error($e->getMessage());
        }

        $row['teaching'] = explode(',',$row['teaching']);
        View::assign('row', $row);
        if(method_exists($this,'get_other')){
            $this->get_other($this->request->param() ?? []);
        }
        return view('', compact('row'));
    }

    /**
     * 获取验证规则
     * @param $post
     * @return array
     */
    public function get_check_rule($post)
    {
        $rule = [
            'introduce|教师介绍' => 'require|max:300',
            'thumb|头像'  => 'require',
            'teaching|授课'  => 'require',

        ];

        return $rule;
    }

}